/* 
 * RedirectUtil.java      1.00    2008-01-11
 *
 * Copyright (c) 2010 monolist. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of monolist.  You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement
 * you entered into with monolist.
 */
package doit.framework.util;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <pre>
 * system      : 공통
 * menu        : 공통
 * source      : RedirectUtil.java
 * description : 리다이렉션 유틸
 * </pre> 
 * @version
 * <pre> 
 * 1.0  2008-01-11  monolist  최초 작성
 * 1.1  
 * </pre>
 */

@SuppressWarnings("unchecked")
public class RedirectUtil {
	private static Log logger = LogFactory.getLog(RedirectUtil.class);
	public static void redirectLogonPage(HttpServletRequest request, HttpServletResponse response) {
		PrintWriter out = null;
		try {
			response.setContentType("text/html;charset=UTF-8");
			out = response.getWriter();
			out.print("<HTML>");
			out.print("<BODY>");
			out.println("<SCRIPT>");
			out.print(" alert(");
			out.print(" '로그인되어 있지 않습니다. 로그인 페이지로 이동합니다.'");
			out.println(" );");
			out.println("	parent.location.href= 'http://localhost';");
			out.println("</SCRIPT>");
			out.print("</BODY>");
			out.print("</HTML>");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}

	public static void redirect(HttpServletRequest request, HttpServletResponse response, Map model, String viewName) {
		boolean makeLog = (Boolean.valueOf(Config.getString("doit.common.log.enable-info")));
		PrintWriter out = null;
		try {
			response.setContentType("text/html;charset=UTF-8");
			out = response.getWriter();
			out.print("<HTML>");
			out.print("<BODY onload='javascript:document.redirectForm.submit();'>");
			out.println("<FORM name='redirectForm' id='redirectForm' method='post' action='"
							+ viewName + "'>");
			Enumeration e = request.getParameterNames();
			boolean isElement = false;
			if (e.hasMoreElements()){
				isElement = true;
				if (makeLog) {
					logger.info("-------------------------------------------------------------------------------------------------------");
					logger.info("|                  key                  	  |                      value                        |");
					logger.info("-------------------------------------------------------------------------------------------------------");
				}
			}
			while (e.hasMoreElements()) {
				String key = (String) e.nextElement();
				if (!request.getParameter(key).equals("") && request.getParameter(key) != null && !key.equals("method")) {
					out.println("<input type='hidden' id='" + key + "' name='" + key + "' value='" + request.getParameter(key) + "'>");
					String value = request.getParameter(key);
					String emptyKey = "";
					String emptyKeyValue = "";
					for (int i = 0 ; i < 47 - key.length() ; i++) {
						emptyKey += " ";
					}
					for (int i = 0 ; i < 50 - value.length() ; i++) {
						emptyKeyValue += " ";
					}
					if (makeLog) 
						logger.info("| " + key + emptyKey + " | " + value + emptyKeyValue + "|");				
				}
			}
			String resultKey = "resultValue";
			String resultValue = new String();
			if (model.get(resultKey) != null) {
				String[] result = String.valueOf(model.get(resultKey)).split(Constants.VL_DELIMETER);
				if (result.length > 1) {
					resultKey = result[0];
					resultValue = result[1];
				} else {
					resultValue = String.valueOf(model.get(resultKey));
				}
				out.println("<input type='hidden' id='" + resultKey + "' name='" + resultKey + "' value='" + resultValue + "'>");
				String emptyKey = "";
				String emptyKeyValue = "";
				for (int i = 0 ; i < 47 - resultKey.length() ; i++) {
					emptyKey += " ";
				}
				for (int i = 0 ; i < 50 - resultValue.length() ; i++) {
					emptyKeyValue += " ";
				}
				if (makeLog)
					logger.info("| " + resultKey + emptyKey + " | " + resultValue + emptyKeyValue + "|");	
			}
			if (isElement) {
				if (makeLog)
					logger.info("-------------------------------------------------------------------------------------------------------\n\n");
			}
			out.println("</FORM>");
			out.print("</BODY>");
			out.print("</HTML>");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
}
